package com.yc.web.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oyj.dao.DBHelper;

@WebServlet("/cart")
public class CartServlet extends BasicServlet {
	private DBHelper db = new DBHelper();

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/plain;charset=utf-8");
		
		//得到op
		String op = req.getParameter("op");
		
		//开始判断
		if("getCart".equals(op)){
			getCart(req,resp);
		}else if("update".equals(op)){
			update(req,resp);
		}else if("add".equals(op)){
			add(req,resp);
		}else if ( "getCartInfo".equals(op) ){
			getCartInfo(req,resp);
		}else if ("delGoods".equals(op)){
			delGoods(req,resp);
		}else if("changeNumByCno".equals(op)){
			changeNumByCno(req,resp);
		}
	}

	private void changeNumByCno(HttpServletRequest req, HttpServletResponse resp) {
		int cno = Integer.parseInt(req.getParameter("cno"));
		int num = Integer.parseInt(req.getParameter("num"));
		String sql = "update cartinfo set num =? where cno = ?";
		this.send(resp, db.doUpdate(sql, num,cno));
	}

	private void delGoods(HttpServletRequest req, HttpServletResponse resp) {
		int cno = Integer.parseInt(req.getParameter("cno"));
		String sql = "delete from cartinfo where cno = ?";
		this.send( resp, db.doUpdate(sql, cno) );
	}

	private void getCartInfo(HttpServletRequest req, HttpServletResponse resp) {
		Map<String,Object> user = (Map<String, Object>) req.getSession().getAttribute("loginUser");
		int mno = Integer.parseInt(user.get("mno") + "");
		
		String sql = "select * from cartinfo c,goodsinfo  g where c.gno = g.gno and c.mno = ?";
		this.send(resp, db.findAll(sql, mno));
	}

	//添加
	private void add(HttpServletRequest req, HttpServletResponse resp) {
		int num = Integer.parseInt(req.getParameter("num"));
		int gno = Integer.parseInt(req.getParameter("gno"));
		//mno从session里面取
		Map<String,Object> user = (Map<String, Object>) req.getSession().getAttribute("loginUser");
		int mno = Integer.parseInt(user.get("mno") + "");
		
		String sql = "insert into cartinfo values(null,?,?,?)";
		int result = db.doUpdate(sql, mno,gno,num);
		if(result > 0){
			this.send(resp, 1);
		}else{
			this.send(resp, 0);
		}
	}

	//修改数量
	private void update(HttpServletRequest req, HttpServletResponse resp) {
		int num = Integer.parseInt(req.getParameter("num"));
		int gno = Integer.parseInt(req.getParameter("gno"));
		//mno从session里面取
		Map<String,Object> user = (Map<String, Object>) req.getSession().getAttribute("loginUser");
		int mno = Integer.parseInt(user.get("mno") + "");
		
		String sql = "update cartinfo set num = num +? where mno = ? and gno = ?";
		int result = db.doUpdate(sql, num,mno,gno);
		if(result > 0){
			this.send(resp, 1);
		}else{
			this.send(resp, 0);
		}
		
	}

	private void getCart(HttpServletRequest req, HttpServletResponse resp) {
		int mno = Integer.parseInt(req.getParameter("mno"));
		String sql = "select * from cartinfo where mno = ? ";
		
		List<Map<String, Object>> list = db.findAll(sql, mno);
		this.send(resp, list);
		
	}
	
}
